package db;

import java.math.BigDecimal;
import java.sql.*; 

/**
 * This class encapsulates a subset of JDBC functions. It includes opening and
 closing the connection, execution of select and action queries, and transaction
 processing.
 
 * @modelguid {F6EC43EA-1AF0-46B3-8A9A-0503A4E9623B}
 */
public final class Query {
	public static String qryGetLastAdded = "SELECT LAST_INSERT_ID()";
	/** @modelguid {BECE23A7-6986-4E8C-9A67-EE7FA6C02867} */
	private Connection con;
	/** @modelguid {8376F55C-CD2E-4AE7-91FE-83AAAB4E91EA} */
	private Statement stmt;	
/**
 * Constructor 
 * @modelguid {6C771E60-5967-4B46-8B49-BDBA142E377A}
 */
public Query() { }
/**
 * Close the database connectivity.
 * @modelguid {E972F4F0-0100-47B7-9DBB-8078A4CC7386}
 */
public void close() throws SQLException {
					
		if (con != null)
		{
			if (con.getAutoCommit()==false)
				con.commit();		
			if (stmt!=null) stmt.close();
			ConnectionManager.releaseConnection(con);
		}
		else if (stmt!=null) stmt.close();
			
}
/**
 * This method to execute Select queries.
 * @modelguid {72F25077-5F0C-4E3A-95D2-27E7B8A0310D}
 */
public static void emptyTable(String table) throws Exception 
{
	String sql = "DELETE FROM "+table;
	Query q = new Query();
	q.open();
	try
	{
		q.executeUpdate(sql);
	}		
	finally
	{
		q.close();
	}
}
public ResultSet executeQuery(String strQuery) throws SQLException{
	ResultSet RS;
	RS = stmt.executeQuery(strQuery);
	//Log.log("Excecute Query");
	return RS;
}

/**
 * This method is to execute Insert,Delete and Update queries.
 * The method returns the number of rows that has changed; that is,
 * if no row changed, it returns 0
 * @modelguid {965A1E7A-8636-4E72-9B35-E641CA0CDCB8}
 */
public int executeUpdate(String strQuery) throws SQLException {
	int iResult;
	iResult = stmt.executeUpdate(strQuery);
	return iResult;
	}
/**
 * This method is to activate stored procedures that execute Insert,Delete and Update queries.
 * The method returns the number of rows that has changed; that is,
 * if no row changed, it returns 0
 */	
/*
public int executeCall(String proc,String par[]) throws SQLException
{
	int iResult;
	String sql="{call "+proc+"(";
	for (int i=0;i<par.length;i++)
	{		
		if (par.length-i==1)
			sql+=par[i];
		else sql+=par[i]+",";
		Log.log("Parameter number "+i+1+" set");
	}
	sql+=")}";
	Log.log("sql is: "+sql);
	CallableStatement cs = con.prepareCall(sql);	
	boolean b = cs.execute();
	if (b) return 1;
	else return 0; 
}
	 * @modelguid {781AF9E0-4F85-4737-B7D8-D10C5DCAB6CA}
*/
public int executeCall(String proc,String par[]) throws SQLException
{
	//int iResult;
	String sql="{call "+proc+"(?,?,?)}";
	CallableStatement cs = con.prepareCall(sql);
	cs.setBigDecimal(1,new BigDecimal(par[0]));
	cs.setBigDecimal(2,new BigDecimal(par[1]));
	cs.setBigDecimal(3,new BigDecimal(par[2]));
	//cs.setBigDecimal(4,new BigDecimal(par[3]));
	
	//Log.log("sql is: "+sql);
		
	boolean b = cs.execute();
	if (b) return 1;
	else return 0; 
}
	/** @modelguid {522CC6C6-E7FE-430C-AE48-2FFA8FCD2F58} */
public int executeCall(String proc) throws SQLException
{
	//int iResult;
	String sql="{call "+proc+"}";
	//Log.log("sql is: "+sql);
	CallableStatement cs = con.prepareCall(sql);	
	boolean b = cs.execute();
	if (b) return 1;
	else return 0; 
}
/**
 * Opens the database connectivity.
 * @modelguid {9F566322-1D09-483D-BB28-28B86060DD83}
 */
public void open() throws SQLException,ClassNotFoundException {
	con = ConnectionManager.getConnection();
	//com.gaeb.search.util.Log.log("connected");
	//Log.log("Query opened");
	//con = DriverManager.getConnection(url);
	stmt = con.createStatement();
}


/**
 * begin a transaction so that multiple dependent queries can be
 executed while maintaining data integrity
	 * @modelguid {B4EB2367-E8A6-49C5-B0C0-E7F6B3CB0C63}
 */
/*
public void beginTransaction() throws SQLException {
	con.setAutoCommit(false);
}
/**
 * the transaction is commited so the effect of the queries become permanent.
 * normal mode is returned where AutoCommit=true
 * @modelguid {701F20F1-9975-4F26-9F5D-8779E3268F29}
 */
/*
public void endTransaction() throws SQLException {
	con.commit();
	con.setAutoCommit(true);
}
*/
/**
 * the transaction is cancelled so that queries within the transaction has no effect.
 * normal mode is returned where AutoCommit=true
 * @modelguid {C576CC05-0D82-4A2B-8063-0989E5A68B5B}
 */
/*
public void cancelTransaction() throws SQLException {
	con.rollback();
	con.setAutoCommit(true);
}
*/
}
